package com.ssish.saas.eccs.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import com.ssish.saas.eccs.model.Claims;
import com.ssish.saas.eccs.model.SymUser;
import com.ssish.saas.eccs.service.AuthorizationService;
import com.ssish.saas.eccs.util.JsonResult;
import com.ssish.saas.eccs.util.PrinttemplateConfig;
import com.ssish.saas.eccs.vo.ClaimsVo;
import com.ssish.saas.eccs.vo.LoginUserVo;




@Api("事先授权接口文档")
@RefreshScope
@RestController
@RequestMapping("/authorization")
public class AuthorizationController extends BaseController {
    @Value("${eccs.user.key}")
    private String userKey;
    
    @Value("${eccs.print.url}")
	private String printUrl;
	
	    @Autowired
	    private AuthorizationService authorizationService;
	    
	    @ApiOperation("查询事先授权接口")
	    @PostMapping("/queryAuthorInfo")
	    public  JsonResult<List<Map<String,Object>>> queryAuthorInfo(@RequestBody ClaimsVo claimsVo ) {
	    	System.out.println("查询开始。。。。。");
	    	List<Map<String,Object>> claimsList=new ArrayList<Map<String,Object>>();
	    	Map<String,Object> resultMap=new HashMap<String, Object>();
	    	try {
				    String patientName=claimsVo.getPatientName();//就诊人
			    	BigDecimal cardNo=claimsVo.getCardNo();//保险卡号
			    	String newIc =claimsVo.getNewIc();  //证件号
			    	Date admissionDateBegin=claimsVo.getAdmissionDateBegin(); // 预约就诊日期起期
			    	Date admissionDateEnd=claimsVo.getAdmissionDateEnd(); // 预约就诊日期起期
			    	String policyNo=claimsVo.getPolicyNo();//保单号
			    	
			    	Map<String,Object> parm=new HashMap<String, Object>();
			    	if(patientName!=null&&!"".equals(patientName)){
			    		parm.put("patientName", patientName);
			    	}
			    	
			    	if(cardNo!=null&&!"".equals(cardNo)){
			    		parm.put("cardNo", cardNo);
			    	}
			    	
			    	if(newIc!=null&&!"".equals(newIc)){
			    		parm.put("newIc", newIc);
			    	}
			    	if(admissionDateBegin!=null&&!"".equals(admissionDateBegin)){
			    		parm.put("admissionDateBegin", admissionDateBegin);
			    	}
			    	if(admissionDateEnd!=null&&!"".equals(admissionDateEnd)){
			    		parm.put("admissionDateEnd", admissionDateEnd);
			    	}
			    	
			    	if(policyNo!=null&&!"".equals(policyNo)){
			    		parm.put("policyNo", policyNo);
			    	}
			    	claimsList=authorizationService.queryAuthorInfoList(parm);
	    	 
	    	} catch (Exception e) {
	    		return JsonResult.err("查询失败", JsonResult.EXCEPTION);
			}
	    	 return JsonResult.ok(claimsList);
	    	
	    }
   
        //根据授权编号获取单条授权的详细信息
	    @ApiOperation("查询事先授权接口")
	    @PostMapping("/queryAuthorDetilById")
	    public  JsonResult<Map<String,Object>> queryAuthorDetilById(@RequestBody ClaimsVo claimsVo) {
	    	Integer claimsId=claimsVo.getClaimsId();
	    	Map<String,Object> claimsMap=authorizationService.queryAuthorDetilById(claimsId);
	        return JsonResult.ok(claimsMap);
	    }
	    
	    //根据授权编号获取单条授权的详细信息
	    @ApiOperation("导出事先授权数据接口")
	    @RequestMapping("/exportData")
	    public  void exportData(@RequestBody ClaimsVo claimsVo,HttpServletResponse respones) throws Exception {
	    	    System.out.println("导出数据");
	    	    String patientName=claimsVo.getPatientName();//就诊人
		    	BigDecimal cardNo=claimsVo.getCardNo();//保险卡号
		    	String newIc =claimsVo.getNewIc();  //证件号
		    	Date admissionDateBegin=claimsVo.getAdmissionDateBegin(); // 预约就诊日期起期
		    	Date admissionDateEnd=claimsVo.getAdmissionDateEnd(); // 预约就诊日期起期
		    	String policyNo=claimsVo.getPolicyNo();//保单号
		    	
		    	Map<String,Object> parm=new HashMap<String, Object>();
		    	if(patientName!=null&&!"".equals(patientName)){
		    		parm.put("patientName", patientName);
		    	}
		    	
		    	if(cardNo!=null&&!"".equals(cardNo)){
		    		parm.put("cardNo", cardNo);
		    	}
		    	
		    	if(newIc!=null&&!"".equals(newIc)){
		    		parm.put("newIc", newIc);
		    	}
		    	if(admissionDateBegin!=null&&!"".equals(admissionDateBegin)){
		    		parm.put("admissionDateBegin", admissionDateBegin);
		    	}
		    	if(admissionDateEnd!=null&&!"".equals(admissionDateEnd)){
		    		parm.put("admissionDateEnd", admissionDateEnd);
		    	}
		    	
		    	if(policyNo!=null&&!"".equals(policyNo)){
		    		parm.put("policyNo", policyNo);
		    	}
		    	authorizationService.exportData(parm, respones);
	    }
	    
	    
	    
	    @ApiOperation("授权信息的文件上传据接口")
	    @RequestMapping("/upload")
	    public JsonResult<String> uploadfile(@RequestBody ClaimsVo claimsVo,HttpServletResponse respones){
	    	return null;
	    }
	    
	    
	    @ApiOperation("打印文件接口")
	    @RequestMapping("/print")
	    public JsonResult<Map<String,String>> printfile(@RequestBody ClaimsVo claimsVo,HttpServletRequest request,  HttpServletResponse respones){
	    	  SymUser user=(SymUser)request.getSession().getAttribute(userKey);//获取登录人的信息
	    	  Integer claimsId=claimsVo.getClaimsId();
	    	  Claims claims=authorizationService.selectClaimsById(claimsId);
	    	  Date lastClaimsStatusDate=claims.getLastClaimsStatusDate();
	    	  //String userId=user.getUsrid();//登录人信息
	    	  String userId="test";
	    	  String printType=claimsVo.getPrintType();  //要打印的文件类型
	    	  if("PAA".equals(printType)){
	    		  printType=printType+"-"+claims.getAdmissionType();
	    	  }
	    	  String claimsStatus=claims.getClaimsStatus();
	    	  Map<String,Object>  map=new HashMap<String,Object>();//printUrl
	    	  map.put("printUrl", printUrl);
	    	  map.put("cid", claimsId);
	    	  map.put("trxdt", lastClaimsStatusDate);
	    	  map.put("csts", claimsStatus);
	    	  map.put("usid", userId);
	    	  map.put("docCD", printType);
	    	  String printUrl= PrinttemplateConfig.getauthorPrintUrl(map);
              Map<String,String> maps=new HashMap<String, String>();
	    	  maps.put("printUrl", printUrl);
	    	  return JsonResult.ok(maps);
	    }

}
